using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using com.cruxomatic.DTOs;
using com.cruxomatic.BusinessObjects;
using com.cruxomatic.BusinessObjects.DatabaseQueries;
using com.cruxomatic.exceptions;
 

namespace com.cruxomatic.BusinessFacades
{
    public class MasterLocationFacade
    {
        public List<MasterLocationInformation> GetAllMasterLocation()
        {
            Queries qry = new Queries();
            IEnumerable<MasterLocationInformation>  info =
                        from MasterLocationInfo in qry.GetAllMasterLocation()
                        select ClassConverter.CopyMasterLocationToMasterLocationInformation(MasterLocationInfo);
            return info.ToList();

        }

        public MasterLocationInformation GetSingleMasterLocation(int MasterLocationID)
        {
			if (MasterLocationID == 0)return new MasterLocationInformation();

            Queries qry = new Queries();
            MasterLocationInformation info = ClassConverter.CopyMasterLocationToMasterLocationInformation(qry.GetSingleMasterLocation(MasterLocationID));                
            return info ;
        }


        public void DeleteMasterLocation(int MasterLocationID)
        {
            Queries qry = new Queries();
            MasterLocation info = qry.GetSingleMasterLocation(MasterLocationID);
            qry.DeleteMasterLocation(info);
            qry.SubmitChanges();
        }

        public void SaveMasterLocation(MasterLocationInformation argMasterLocation)
        {
            Queries query = new Queries();
            MasterLocation dbMasterLocation = new MasterLocation ();
            if (argMasterLocation.MasterLocationID != 0)
            { 
			    dbMasterLocation = query.GetSingleMasterLocation(argMasterLocation.MasterLocationID);
                dbMasterLocation = ClassConverter.CopyMasterLocationInformationToMasterLocation(argMasterLocation, dbMasterLocation);
            }
            else
            {
				dbMasterLocation = ClassConverter.CopyMasterLocationInformationToMasterLocation(argMasterLocation, dbMasterLocation);
                query.InsertMasterLocation(dbMasterLocation);
	     	}
			   query.SubmitChanges();
        }


        public List<MasterLocationInformation> GetMasterLocationsByCompany(int CompanyID)
        {
            Queries query = new Queries();
            IEnumerable<MasterLocationInformation> locations =
                from alllocations in query.GetMasterLocationsByCompany(CompanyID)
                select ClassConverter.CopyMasterLocationToMasterLocationInformation(alllocations);
            return locations.ToList();
        }
		
      
    }
}